Part Number Hot Search : 
HD74LS16 0J100 LC6527H 104JS 4MTCX SMA6862M KBPC350 15N06VL
Product Description
Full Text Search
 

To Download AN1527 Datasheet File

  If you can't view the Datasheet, Please click here to try to view without PDF Reader .  
 
 


  Datasheet File OCR Text:
  AN1527/0702 1/14 AN1527 application note developing a usb smartcard reader with st7scr by microcontroller division applications introduction this document describes a firmware implementation developed by stmicroelectronics for a usb smart card reader. this firmware is for the st7scr microcontroller and can be used with the associated smart card reader board available from stmicroelectronics. it is divided into 4 parts: C universal serial bus (usb) management: this is the usb library which manages the usb hardware. C chip card interface device (ccid) implementation: this contains high level functions for us- ing the usb in compliance with ccid specifications (messages for bulk-in, bulk-out, inter- rupt and class requests) C interface device (ifd) implementation: this contains high level functions for iso 7816 spec- ification implementation and the smartcard-specific command interpreter. C 7816 uart smartcard interface (crd) management: this contains low level functions for the hardware management of the 7816 uart smartcard interface (crd). the main loop polls usb transactions (functions in ccid_usb.c) using a state machine process: C usb_polling() function from library for usb low level and endpoint 0 management. C receive usb bulk-out message (ccid part). C execute the function corresponding to the bulk-out message (ifd part). C send usb interrupt in message (ccid part) if necessary. C send usb bulk-in response (ccid part). the bulk-out/in messages are managed by a state machine. when the bulk-out message re- ception is completed, the message function is executed and returns only after the completion of the action to be done in the ifd part. as this may take several 10 ms (e.g. atr reading), the response is sent to the host by bulk-in message. the bulk-in/out messages are received in several transactions with a single transaction at every main loop if the usb endpoint is avail- able. if there are any interrupt messages, they are sent just before the bulk-in message is sent. specific ccid class requests are managed by the setup management function in the usb li- brary (user_usb_setup() function). 1
2/14 developing a usb smartcard reader with st7scr 1 ccid implementation 1.1 generalities the chip card interface device (ccid) firmware implementation conforms to the universal serial bus device class specification for usb chip/smart card interface devices revision 1.0. it contains two files: C ccid_usb.c: this file contains the usb - ccid interface functions used to manage the state machine that generates bulk-in/bulk-out messages. C ifd_ccid.c: this file contains the pc_to_rdr_xxx() and rdr_to_pc_xxx() message func- tions for the ccid. the following commands are not supported but can be added easily: C pc_to_rdr_secure, C pc_to_rdr_setdatarateandclockfrequency, C pc_to_rdr_toapdu, C pc_to_rdr_mechanical. this ccid part uses a 271-byte buffer for all messages. messages are composed of two parts: C header (10 bytes: fixed size) C data (up to 261 bytes). the size is based on the largest message managed at the short apdu transaction level. due to this large size, the st7scr can have only 1 buffer and cannot store more than one mes- sage at a given time. the ccid_bulkoutmessage() function uses several bulk-out usb transactions to verify the message header and store the entire message in the buffer. when the message reception is completed, the ccid_dispatchmessage() function identifies the message type and calls the corresponding function (pc_to_rdr_xxx()) for processing. then, the ifd part of the firmware executes a command on the icc if necessary. this com- mand is included in the data field of the buffer. the pc_to_rdr_xxx() function receives from the ifd the answer to the message in the buffer data field and returns an error code to be in- cluded in the bulk-in response. the rdr_to_pc_xxx() function, which corresponds to the pc_to_rdr_xxx() previously exe- cuted, is launched and enters the correct values for error and status codes in the header. at this time, the bulk-in message is ready to be sent. but before the sending, the ccid_intmessage() function is executed to detect any hardware problems or if a slot change has occurred. in this case, the corresponding interrupt message is sent to the host. 2
3/14 developing a usb smartcard reader with st7scr to finish the main loop, the ccid_bulkinmessage() function sends the bulk-in message as an answer to the previous bulk-out message. the message is sent in a process that requires sev- eral bulk-in transactions (same concept as for the bulk-out process). in addition, the abort request is implemented through the ccidclassrequestabort() function called in the usb_polling() function. for this purpose, the usb_polling() function is called in the pc_to_rdr_xxx() function. when this request is received from endpoint 0, a flag is set and the command sequence number is memorized. all commands are aborted until the next pc_to_rdr_abort command with the correct se- quence number is received. 1.2 function description n void ccid_init(void) this function initializes the flags and status variables for the state machine process. n void ccid_init_it(void) this function initializes the state machine and switches off the vcccard after a usb reset done by the pc host. it can only be called by an interrupt routine. n void ccid_suspend_it(void) this function initializes the state machine and the card interface hardware after a resume from usb suspend mode. n void ccid_bulkoutmessage(void) this function manages the state machine during usb bulk out message reception. it fills the message buffer with a maximum of 271 bytes. n void ccid_bulkinmessage(void) this function manages the state machine during the usb bulk in message transmission. it sends the buffer content as a message with a maximum of 271 bytes. n void ccid_dispatchmessage(void) this function identifies the usb bulk out received message and calls the corresponding func- tions to process it : pc_to_rdr_xxx() and rdr_to_pc_xxx(). n void ccid_intmessage(void) this function verifies the slot status and sends an interrupt in message if needed.
4/14 developing a usb smartcard reader with st7scr n void ccidclassrequestabort(void) this function is called from the user_usb_setup() function to process an abort request. n unsigned char pc_to_rdr_iccpoweron(void) this function verifies the pc_to_rdr_iccpoweron command format and calls the ifd_iccpoweron() function (from interface device level). if ifd_iccpoweron() returns no error, the message header is filled with the atr length. this pc_to_rdr function returns an error code. n unsigned char pc_to_rdr_iccpoweroff(void) this function verifies the pc_to_rdr_iccpoweroff command format, switches off the smartcard power supply and returns the noerror code. n unsigned char pc_to_rdr_getslotstatus(void) this function verifies the pc_to_rdr_getslotstatus command format, verifies the slot hardware state and returns the corresponding error code. n unsigned char pc_to_rdr_xfrblock(void) this function verifies the pc_to_rdr_xfrblock command format, checks the slot status and returns an error if needed. if there is no error, it calls the ifd_xfrblock() function (from the interface device level). the message buffer contains the data to be transferred and is updated by this ifd function. this pc_to_rdr function returns an error code. n unsigned char pc_to_rdr_getparameters(void) this function verifies the pc_to_rdr_getparameters command format, checks the slot hardware state and returns the corresponding error code. n unsigned char pc_to_rdr_resetparameters(void) this function verifies the pc_to_rdr_resetparameters command format, fills the message buffer with the default values of the reader parameters and calls the ifd_setparameters() function (from the interface device level). this pc_to_rdr function re- turns an error code. n unsigned char pc_to_rdr_setparameters(void) this function verifies the pc_to_rdr_setparameters command format and calls the ifd_setparameters() function (from the interface device level). this pc_to_rdr function re- turns an error code.
5/14 developing a usb smartcard reader with st7scr n unsigned char pc_to_rdr_escape(void) this function verifies the pc_to_rdr_escape command format and calls the ifd_escape() function (from the interface device level). this pc_to_rdr function returns an error code (from ifd_escape(), for example). n unsigned char pc_to_rdr_iccclock(void) this function verifies the pc_to_rdr_iccclock command format and calls the ifd_setclock() function (from the interface device level). this pc_to_rdr function returns an error code (from ifd_setclock(), for example). n unsigned char pc_to_rdr_abort(void) this function verifies the pc_to_rdr_abort command format, checks the abort condi- tions and sets the corresponding flag. it returns the corresponding error code. n void rdr_to_pc_datablock(unsigned char errorcode) this function fills the whole command buffer header including the error code given as input to prepare the rdr_to_pc_datablock message. n void rdr_to_pc_slotstatus(unsigned char errorcode) this function fills the whole command buffer header including the error code given as input to prepare the rdr_to_pc_slotstatus message. n void rdr_to_pc_parameters(unsigned char errorcode) this function fills the whole command buffer header including the error code given as input to prepare the rdr_to_pc_parameters message and call the ifd_getparameters() func- tion to fill the message data field. n void rdr_to_pc_escape(unsigned char errorcode) this function fills the whole command buffer header including the error code given as input to prepare the rdr_to_pc_escape message.
6/14 developing a usb smartcard reader with st7scr 2 ifd implementation the interface device (ifd) implementation firmware contains one file: C ifd_protocol.c: this is the iso 7816 implementation for protocol types t=0 and t=1 and for character, tpdu and short apdu levels. the protocol type is also managed at the crd level by the interrupt system for data reception from the icc. for this release, only the t=0 with character level has been implemented in the current firmware. this part has to be modified in depth to integrate the secure function and other communication types and levels. all the functions in this part are called by a ccid function. these functions are typically those where the reader interprets the messages sent to/from the icc and manages the parameters for icc communication (speed, type, etc....). an ifd structure is used to save all the current parameters in compliance with ccid specifi- cations. the following functions are used for icc and parameter management: C ifd_init(): this sets the parameter structure to the default value. C ifd_getparameters(): this returns the current parameters in a buffer. C ifd_setparameters(): this enters the new parameters in the ifd structure and programs the crd for use with the new configuration. C ifd_applyparametersstructure(): this programs the crd to use the configuration de- scribed by the ifd structure. C ifd_updateconvparameterstructure(): this changes the convention parameter of the structure with the value programmed in the crd. C ifd_iccpoweron(): this switches on the v cc card with automatic voltage selection and re- turns the answer to reset (atr) variable. C ifd_xfrblock(): this sends a command and receives the answer with current parameters. C ifd_xfrchart0(): this is given by the ifd structure. (character level and t=0 type). C ifd_escape(): this is dedicated to specific communication between reader and pc. (see ccid spec). C ifd_setclock(): this changes the clock state as configured in the ifd structure.
7/14 developing a usb smartcard reader with st7scr 3 iso 7816 uart library (crd) 3.1 general introduction the aim of this library is to provide the user with a set of functions for using the smartcard in- terface (iso7816-3 uart interface) to directly access the hardware and to communicate with an integrated circuit card (icc) through a few simple functions. this library is composed of 4 files: C crd.c C crd.h C int_crd.c C int_crd.h the crd.x files provide the general functions for the main loop program. the int_crd.x files are used for interrupt management. the transactions between the st7 microcontroller and the icc are performed by the icc_xxx functions. these functions poll flags waiting for the end of the transaction. the transaction it- self and the flag settings are managed by interrupts. 3.2 smartcard interface functions the smartcard interface (crd) functions described below are very low level functions used to interact directly with the hardware. n void crd_init(void) this function resets and initialize the crd and the local software flags. n unsigned char crd_gethwerror(unsigned char * phwerrorcode) this function checks the hardware state and returns one of the following error codes: C slot_no_error C sloterror_hw_error C sloterror_icc_mute (no card present) if the function returns sloterror_hw_error, phwerrorcode will contain one of the following error codes: C hardwareerrorcode_overcurrent C hardwareerrorcode_voltageerror C hardwareerrorcode_overcurrent_it C hardwareerrorcode_voltageerror_it note: in case of a hardware error, the slot is inactivated (vcc card off).
8/14 developing a usb smartcard reader with st7scr n unsigned char crd_vccon(unsigned char voltage) this function switches on the v cc card with value given by the voltage variable. there are three possible voltage values: C crd_voltage18v (for 1.8 volts) C crd_voltage3v (for 3.0 volts) C crd_voltage5v (for 5.0 volts) this function returns one of the following error codes: C slot_no_error C sloterror_hw_error C sloterror_icc_mute (no card present) n void crd_vccoff(void) this function switches off the v cc card. the inactivating sequence is managed by hard- ware. n void crd_setmode(unsigned char mode) this function sets the operating mode of the crd. there are two operating modes: C crd_manualmode C crd_uartmode n void crd_setconvention(unsigned char convention) this function sets the convention used by the crd to communicate with the icc. there are two conventions: C crd_directconv C crd_inverseconv n unsigned char crd_getslotstatus(void) this function returns the current status of the slot (only 1 slot is possible with this device). there are three possible states for the slot: C crd_notpresent C crd_presentinactive C crd_presentactive
9/14 developing a usb smartcard reader with st7scr n unsigned char crd_getconvention(void) this function returns the convention currently in use in the crd. there are two conventions: C crd_directconv C crd_inverseconv n unsigned char crd_getclockstatus(void) this function returns the status of the crd clock signal. there are three possible states for the clock signal: C crd_clockrunning C crd_clockstoppedlow C crd_clockstoppedhigh n unsigned char crd_setclock(unsigned char clockstate) this function modifies the state of the crd clock signal. clockstate can be one of the fol- lowing ones: C crd_clockrunning C crd_clockstoppedlow C crd_clockstoppedhigh C crd_clockstoppedloworhigh this function returns one of the following error codes: C slot_no_error C sloterror_icc_mute n unsigned char crd_setetu(unsigned int etu, unsigned char comp) this function sets the elementary time unit registers to the value of the etu variable. the value of this variable must be a positive integer between 12 and 2048. if the etu value is 2048, the hardware registers are cleared and the crd behaves as if it was set to 2048. the comp value can be 0 or 1. it is written to the comp bit of register crdetu1. for more information, please refer to the st7scr datasheet. this function will return 0 if the crdetu registers are correct and 0xff if the input values are incorrect. n void crd_setguardtime(unsigned int guardtime) void crd_setwaitingtime(unsigned long waitingtime) these functions enter positive integer values in the hardware timer registers.
10/14 developing a usb smartcard reader with st7scr n void crd_startwaitingtime(void) this function starts the waiting timer. the bwaitingtimeflag software flag is set by an inter- rupt when the count is finished. for more information, please refer to the crd.h file. this counter is used by the icc level functions to determine the end of the transmission. n void crd_stopwaitingtime(void) this function stops the waiting timer and resets the corresponding flag. n void crd_waitingtime(unsigned long waitingtimeinetu) this function creates a wait loop for waitingtimeinetu. the waiting time is: C waitingtimeinetu * etu this function is used to set the v cc card stabilization delay or delays during the reset proce- dure. the etu value is set by the crd_setetu function. n void crd_initreceive(unsigned int receivebufsize, unsigned char * pbuffer) this function will configure the crd for reception and begin the process. C pbuffer is the address of the buffer where the data will be copied. C receivebufsize is the size of the buffer. if the icc transmits more bytes than the number specified in the receivebufsize variable, data saving is stopped and a flag is set. this reception is done entirely by interrupt processing. n unsigned int crd_numberofbytesreceived(void) this function returns the number of bytes already received during the reception process. n unsigned int crd_endreceive(void) this function stops the reception process and returns the number of bytes received and copied in the buffer. n void crd_inittransmit(unsigned int transmitbufsize, unsigned char * pbuffer) this function configures the crd for transmission and begins the process. C pbuffer is the address of the buffer containing the data to be transmitted. C transmitbufsize is the number of bytes to be transmitted. this transmission is entirely done by interrupt processing. n unsigned int crd_numberofbytestotransmit(void) this function returns the remaining number of bytes to be transmitted.
11/14 developing a usb smartcard reader with st7scr n unsigned int crd_endtransmit(void) this function finishes the transmission by disabling the interrupt. this function returns the re- maining number of bytes to be transmitted. 3.3 interrupt functions the following two functions are executed when an interrupt occurs: n void int_uart(void) this function is used for all crd interrupts: parity, receive, transmit, waitingtime, voltage_error, over_current and transmit_empty. C parity: a flag is set if 4 automatic retries have failed. C waitingtime: a flag is set when a counter overflow is detected. C voltage and current: the corresponding flags are set, the corresponding interrupt is invalidated and the v cc card is switched off. C transmit: the transmit register is loaded with the next byte to be sent. if the last byte is sent, the interrupt is disabled and a flag is set. C receive: this function uses a protocol type flag (software) to identify the received bytes and manage the t=0 and the t=1 special cases. C transmitempty: not used. n void int_carddet(void) this function will set a flag with rebound management (see the time base unit (tbu) func- tion). 3.4 icc functions the following functions execute a global action on the icc. n unsigned char icc_poweronasync(unsigned char voltage) this function executes the power-on sequence in compliance with iso 7816 specifications for asynchronous iccs, including the generation of the rising edge for the reset signal. the v cc card voltage is either 1.8 volts, 3.0 volts or 5.0 volts depending on the value of the voltage variable: C crd_voltage18v (for 1.8 volts) C crd_voltage3v (for 3.0 volts) C crd_voltage5v (for 5.0 volts) this function returns one of the following error codes:
12/14 developing a usb smartcard reader with st7scr C slot_no_error C sloterror_hw_error C sloterror_icc_mute note: at the end of this function, the crd is in manual mode. n void icc_resetasync(void) this function executes a warm reset sequence in compliance with iso 7816 specifications for asynchronous iccs. note: at the end of this function, the crd is in manual mode. n unsigned char icc_getatrasync(unsigned char voltage, unsigned char * preceivebuffer, unsigned int * patrsize) this function executes the power-on or reset sequence and answer to reset reading for an asynchronous icc. the voltage variable can be: C crd_voltage18v (for 1.8 volts) C crd_voltage3v (for 3.0 volts) C crd_voltage5v (for 5.0 volts) note: the voltage variable is used only if the slot status is inactive at the beginning of the function. in this case, the cold answer to reset (atr) is returned. if the icc is already active, a simple reset is generated and a warm atr is returned. C preceivebuffer is the address of the buffer to fill with the atr. C patrsize is the address of the data containing: C input: the size of the receivebuffer in bytes. C output: the length of the atr written in the receivebuffer in bytes. this function returns one of the following error codes: C slot_no_error C sloterror_hw_error C sloterror_icc_mute C sloterror_bad_atr_ts C sloterror_xfr_overrun n unsigned char icc_sendcommandasync(unsigned char * ptransmitbuffer, unsigned int commandsize, unsigned char protocoltype) this function sends a packet of bytes to the icc with the guard time currently in use. C ptransmitbuffer is the address of the buffer containing the data to be sent.
13/14 developing a usb smartcard reader with st7scr C commandsize is the size of the buffer in bytes. C protocoltype is 0 (for t=0) or 1 (for t=1). this variable is used to set a flag tested in the interrupt process at transmission and reception levels. this function will return one of the following error codes: C slot_no_error C sloterror_icc_mute (if transfer not completed) n unsigned char icc_receiveanswerasync(unsigned char * preceivebuffer, unsigned int * panswersize) this function will receive a packet of bytes from the icc with its waiting time already pro- grammed. C preceivebuffer is the address of the buffer to fill with the received data. C panswersize is the address of the data containing: C input: the size of the receivebuffer in bytes. C output: the length in bytes of the received data written in the receivebuffer. this function will return one of the following error codes: C slot_no_error C sloterror_xfr_overrun C sloterror_icc_mute
14/14 developing a usb smartcard reader with st7scr the present note which is for guidance only aims at providing customers with information regarding their products in order for them to save time. as a result, stmicroelectronics shall not be held liable for any direct, indirect or consequential damages with respect to any claims arising from the content of such a note and/or the use made by customers of the information contained herein in connection with their products. information furnished is believed to be accurate and reliable. however, stmicroelectronics assumes no responsibility for the co nsequences of use of such information nor for any infringement of patents or other rights of third parties which may result from its use. no license is granted by implication or otherwise under any patent or patent rights of stmicroelectronics. specifications mentioned in this publicati on are subject to change without notice. this publication supersedes and replaces all information previously supplied. stmicroelectronics prod ucts are not authorized for use as critical components in life support devices or systems without the express written approval of stmicroele ctronics. the st logo is a registered trademark of stmicroelectronics ? 2002 stmicroelectronics - all rights reserved. purchase of i 2 c components by stmicroelectronics conveys a license under the philips i 2 c patent. rights to use these components in an i 2 c system is granted provided that the system conforms to the i 2 c standard specification as defined by philips. stmicroelectronics group of companies australia - brazil - canada - china - finland - france - germany - hong kong - india - israel - italy - japan malaysia - malta - morocco - singapore - spain - sweden - switzerland - united kingdom - u.s.a. http://www.st.com


▲Up To Search▲   

 
Price & Availability of AN1527

All Rights Reserved © IC-ON-LINE 2003 - 2022  

[Add Bookmark] [Contact Us] [Link exchange] [Privacy policy]
Mirror Sites :  [www.datasheet.hk]   [www.maxim4u.com]  [www.ic-on-line.cn] [www.ic-on-line.com] [www.ic-on-line.net] [www.alldatasheet.com.cn] [www.gdcy.com]  [www.gdcy.net]


 . . . . .
  We use cookies to deliver the best possible web experience and assist with our advertising efforts. By continuing to use this site, you consent to the use of cookies. For more information on cookies, please take a look at our Privacy Policy. X